package com.rxy.socketdemo

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    private lateinit var etIp: EditText
    private lateinit var etPort: EditText
    private lateinit var etMessage: EditText
    private lateinit var btnConnect: Button
    private lateinit var btnSend: Button
    private lateinit var btnCheck: Button

    private var socketService: SocketService? = null
    private var isBound = false

    private val serviceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            val binder = service as SocketService.LocalBinder
            socketService = binder.getService()
            isBound = true
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            socketService = null
            isBound = false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        etIp = findViewById(R.id.et_ip)
        etPort = findViewById(R.id.et_port)
        etMessage = findViewById(R.id.et_message)
        btnConnect = findViewById(R.id.btn_connect)
        btnSend = findViewById(R.id.btn_send)
        btnCheck = findViewById(R.id.btn_check) // 新增按钮

        btnConnect.setOnClickListener {
            val ip = etIp.text.toString().trim()
//            val ip = "192.168.0.108"
            val port = etPort.text.toString().toIntOrNull()
//            val port = 8888

            if (ip.isNotEmpty() && port != null) {
                val intent = Intent(this, SocketService::class.java).apply {
                    action = Constants.ACTION_CONNECT
                    putExtra(Constants.EXTRA_IP, ip)
                    putExtra(Constants.EXTRA_PORT, port)
                }
                startService(intent)
                bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
            } else {
                Toast.makeText(this, "请输入正确的IP地址和端口号", Toast.LENGTH_SHORT).show()
            }
        }

        btnSend.setOnClickListener {
            val message = etMessage.text.toString().trim()
            if (isBound) {
                if (message.isNotEmpty()) {
                    socketService?.sendMessage(message)
                } else {
                    Toast.makeText(this, "请输入要发送的消息", Toast.LENGTH_SHORT).show()
                }
            } else {
                Toast.makeText(this, "未连接", Toast.LENGTH_SHORT).show()
            }

        }

        btnCheck.setOnClickListener {
            if (isBound) {
                val status = if (socketService!!.isConnected()) "已连接" else "未连接"
                Toast.makeText(this, "当前状态：$status", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "未连接", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (isBound) unbindService(serviceConnection)
    }
}

